﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models.LyModel;
using NPOI.HSSF.Record.Chart;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class SupplierTransactionService : BaseService, ISupplierTransactionService
    {
        private readonly ISupplierTransactionRepository _supplierTransactionRepository;
        private readonly ISupplierRepository _supplierRepository;
        private readonly IUnitOfWork unitOfWork;
        public SupplierTransactionService(IMapper mapper, ISupplierTransactionRepository supplierTransactionRepository, ISupplierRepository supplierRepository, IUnitOfWork unitOfWork) : base(mapper)
        {


            _supplierTransactionRepository = supplierTransactionRepository;

            _supplierRepository = supplierRepository;
            this.unitOfWork = unitOfWork;
        }

        public bool AddOrModefiySupplierTransaction(SupplierTransaction model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _supplierTransactionRepository.UpdEntity(model);
            }
            else
            {
                _supplierTransactionRepository.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelSupplierTransaction(long supplierTransactionId)
        {
            var Quote = _supplierTransactionRepository.QueryEntityById(supplierTransactionId);
            if (Quote == null)
            {
                return false;
            }
            _supplierTransactionRepository.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }
        public SupplierTransaction? GetSupplierTransaction(long supplierTransactionId)
        {
            return _supplierTransactionRepository.QueryEntityById(supplierTransactionId);
        }

        public List<SupplierTransaction> GetSuppliersDetailDown()
        {
            var list = _supplierTransactionRepository.GetAllEntityList();
            return list;
        }




        public List<SupplierTransaction> GetSupplierTransactionLoss(DateTime? startDate, DateTime? endDate, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {

            var list = _supplierTransactionRepository.GetEntityAlls().Where(d => d.TransactionType == 4);
            if (startDate != null)
            {
                list = list.Where(d => d.TransactionDate >= startDate);
            }
            if (endDate != null)
            {
                list = list.Where(d => d.TransactionDate <= endDate);
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.CreateDate);
            return list.ToList();

        }

        public List<SupplierTransaction> GetSupplierTransactionQuotation(DateTime? startDate, DateTime? endDate, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _supplierTransactionRepository.GetEntityAlls().Where(d => d.TransactionType == 3);
            if (startDate != null)
            {
                list = list.Where(d => d.TransactionDate >= startDate);
            }
            if (endDate != null)
            {
                list = list.Where(d => d.TransactionDate <= endDate);
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.CreateDate);
            return list.ToList();
        }

        public List<SupplierTransaction> GetSupplierTransactionReconciliation(DateTime? startDate, DateTime? endDate, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _supplierTransactionRepository.GetEntityAlls().Where(d => d.TransactionType == 2);
            if (startDate != null)
            {
                list = list.Where(d => d.TransactionDate >= startDate);
            }
            if (endDate != null)
            {
                list = list.Where(d => d.TransactionDate <= endDate);
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.CreateDate);
            return list.ToList();
        }

        public List<SupplierTransaction> GetSupplierTransactionReturn(DateTime? startDate, DateTime? endDate, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _supplierTransactionRepository.GetEntityAlls().Where(d => d.TransactionType == 1);


            if (startDate != null)
            {
                list = list.Where(d => d.TransactionDate >= startDate);
            }
            if (endDate != null)
            {
                list = list.Where(d => d.TransactionDate <= endDate);
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.CreateDate);
            return list.ToList();
        }
    }
}
